package com.meilingcloud.biobank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.SourceType;
import com.meilingcloud.biobank.mapper.SourceTypeMapper;
import com.meilingcloud.biobank.service.ISourceTypeService;
import com.meilingcloud.common.constant.Constants;
import com.meilingcloud.system.domain.SysCustomForm;
import com.meilingcloud.system.service.ISysCustomFormService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 样本源类型 Service业务层处理
 *
 * @author chenl
 * @date 2021-02-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class SourceTypeServiceImpl extends ServiceImpl<SourceTypeMapper, SourceType> implements ISourceTypeService {

    private final ISysCustomFormService sysCustomFormService;

    @Override
    public List<SourceType> queryList(SourceType sourceType) {
        LambdaQueryWrapper<SourceType> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sourceType.getSourceTypeCode())) {
            lqw.eq(SourceType::getSourceTypeCode, sourceType.getSourceTypeCode());
        }
        if (StringUtils.isNotBlank(sourceType.getSourceTypeName())) {
            lqw.like(SourceType::getSourceTypeName, sourceType.getSourceTypeName());
        }
        if (StringUtils.isNotBlank(sourceType.getSourceTypeDesc())) {
            lqw.eq(SourceType::getSourceTypeDesc, sourceType.getSourceTypeDesc());
        }
        return this.list(lqw);
    }

    @Override
    public List<SourceType> queryListWithForm(SourceType sourceType) {
        List<SourceType> sourceTypes = queryList(sourceType);

        List<SourceType> collect = sourceTypes.stream().map(item -> {
            if (StringUtils.isNotBlank(item.getFormKey())) {
                SysCustomForm sysCustomForm = sysCustomFormService.getByKey(item.getFormKey());
                if (sysCustomForm != null) {
                    item.setFormName(sysCustomForm.getFormName());
                }
            }
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    @Cacheable(value = Constants.SOURCE_TYPE_KEY, key = "#sourceTypeId", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SourceType queryById(Long sourceTypeId) {
        return this.getById(sourceTypeId);
    }

    @Cacheable(value = Constants.SOURCE_TYPE_KEY, key = "#code", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SourceType queryByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<SourceType>().eq(SourceType::getSourceTypeCode, code).last("limit 1"));
    }

    @CacheEvict(value = Constants.SOURCE_TYPE_KEY, allEntries = true)
    @Override
    public boolean updateSourceTypeById(SourceType sourceType) {
        return this.updateById(sourceType);
    }

    @CacheEvict(value = Constants.SOURCE_TYPE_KEY, allEntries = true)
    @Override
    public boolean removeSourceTypeByIds(List<Long> sourceTypeIds) {
        return this.removeByIds(sourceTypeIds);
    }
}
